@echo off
chcp 65001 >nul
title Go Auto Installer

echo ================================
echo    Go Auto Installer
 echo ================================
echo.

:: Set Go version and download URL
set GO_VERSION=1.21.5
set GO_URL=https://golang.google.cn/dl/go%GO_VERSION%.windows-amd64.zip
set DOWNLOAD_DIR=%TEMP%\go_install
set INSTALLER=go%GO_VERSION%.windows-amd64.zip

:: Detect available non-C drives, prefer D or E
set INSTALL_DISK=
set INSTALL_PATH=C:\Go

:: Check if D drive exists
if exist D:\ (set INSTALL_DISK=D) else if exist E:\ (set INSTALL_DISK=E)

:: If non-C drive found, set default install path
if not "%INSTALL_DISK%" == "" (
    set INSTALL_PATH=%INSTALL_DISK%:\Go
    echo Detected available %INSTALL_DISK% drive, default installing Go to %INSTALL_PATH%
    echo.
)

:: User interaction - Select Go version
echo Please enter the Go version to install (press Enter to use default version %GO_VERSION%):
set /p USER_GO_VERSION=
if not "%USER_GO_VERSION%" == "" (
    set GO_VERSION=%USER_GO_VERSION%
    set GO_URL=https://golang.google.cn/dl/go%GO_VERSION%.windows-amd64.zip
    set INSTALLER=go%GO_VERSION%.windows-amd64.zip
    echo Selected Go version: %GO_VERSION%
    echo.
) else (
    echo Using default Go version: %GO_VERSION%
    echo.
)

:: User interaction - Select install disk
set USER_DISK_INPUT=
echo Current default install path: %INSTALL_PATH%
echo Please enter install disk letter (e.g., D, E, etc., press Enter to use default disk):
set /p USER_DISK_INPUT=
if not "%USER_DISK_INPUT%" == "" (
    set INSTALL_DISK=%USER_DISK_INPUT:~0,1%
    set INSTALL_PATH=%INSTALL_DISK%:\Go
    echo Selected install location: %INSTALL_PATH%
    echo.
) else (
    echo Using default install path: %INSTALL_PATH%
    echo.
)

echo [Step 1] Creating download directory...
if not exist "%DOWNLOAD_DIR%" mkdir "%DOWNLOAD_DIR%"

echo [Step 2] Downloading Go %GO_VERSION%...
echo From: %GO_URL%
echo.

:: Try different download methods
set DOWNLOAD_SUCCESS=0

:: Method 1: Using bitsadmin
echo Trying bitsadmin...
bitsadmin /transfer "GoDownload" /download /priority normal "%GO_URL%" "%DOWNLOAD_DIR%\%INSTALLER%"
if %errorlevel% == 0 set DOWNLOAD_SUCCESS=1

:: Method 2: Using certutil if bitsadmin failed
if %DOWNLOAD_SUCCESS% == 0 (
    echo Trying certutil...
    certutil -urlcache -split -f "%GO_URL%" "%DOWNLOAD_DIR%\%INSTALLER%"
    if %errorlevel% == 0 set DOWNLOAD_SUCCESS=1
)

:: Method 3: Using PowerShell if available
if %DOWNLOAD_SUCCESS% == 0 (
    echo Trying PowerShell...
    powershell -Command "Invoke-WebRequest -Uri '%GO_URL%' -OutFile '%DOWNLOAD_DIR%\%INSTALLER%'"
    if %errorlevel% == 0 set DOWNLOAD_SUCCESS=1
)

if %DOWNLOAD_SUCCESS% == 0 (
    echo ERROR: Download failed! Please check your internet connection.
    pause
    exit /b 1
)

echo.
echo [Step 3] Download completed!
echo.

:: Check and create install directory
echo [Step 4] Preparing install directory...
if exist "%INSTALL_PATH%" (
    echo WARNING: Install directory %INSTALL_PATH% already exists!
    echo Cleaning directory...
    rd /s /q "%INSTALL_PATH%" >nul 2>&1
)
mkdir "%INSTALL_PATH%"

echo [Step 5] Extracting Go %GO_VERSION%...
echo Install path: %INSTALL_PATH%
echo.

echo Extracting, please wait...
powershell -Command "Expand-Archive -Path '%DOWNLOAD_DIR%\%INSTALLER%' -DestinationPath '%INSTALL_DISK%:\' -Force"

if %errorlevel% neq 0 (
    echo ERROR: Go extraction failed!
    pause
    exit /b 1
)

echo [Step 6] Go installation completed!
echo.

:: Set environment variables
echo [Step 7] Configuring environment variables...
echo.

:: Check if Go is already in PATH
powershell -Command "if (-not ($env:PATH -like '*%INSTALL_PATH%\bin*')) { [Environment]::SetEnvironmentVariable('PATH', $env:PATH + ';' + '%INSTALL_PATH%\bin', 'Machine') }"

:: Set GOROOT environment variable
powershell -Command "[Environment]::SetEnvironmentVariable('GOROOT', '%INSTALL_PATH%', 'Machine')"

:: Create default GOPATH
set GOPATH=%USERPROFILE%\go
if not exist "%GOPATH%" mkdir "%GOPATH%"
mkdir "%GOPATH%\src" 2>nul
mkdir "%GOPATH%\pkg" 2>nul
mkdir "%GOPATH%\bin" 2>nul

:: Set GOPATH environment variable
powershell -Command "[Environment]::SetEnvironmentVariable('GOPATH', '%GOPATH%', 'User')"

:: Update current session environment variables
set PATH=%PATH%;%INSTALL_PATH%\bin
set GOROOT=%INSTALL_PATH%
set GOPATH=%GOPATH%

echo Environment variables configured:
echo GOROOT: %GOROOT%
echo GOPATH: %GOPATH%
echo.

echo [Step 8] Verifying installation...
echo.

:: Check Go installation
go version >nul 2>&1
if %errorlevel% == 0 (
    echo Go verification: SUCCESS
    go version
) else (
    echo Go verification: FAILED
    echo Please restart command prompt or system.
)

echo.
echo ================================
echo    Installation Summary
 echo ================================
echo Go Version: %GO_VERSION%
echo Install Path: %INSTALL_PATH%
echo GOROOT: %GOROOT%
echo GOPATH: %GOPATH%
echo.
echo Please restart command prompt to ensure
echo environment variables take effect.
echo.

:: Clean up
echo [Step 9] Cleaning up temporary files...
if exist "%DOWNLOAD_DIR%" (
    del "%DOWNLOAD_DIR%\%INSTALLER%" >nul 2>&1
    rmdir "%DOWNLOAD_DIR%" >nul 2>&1
)

echo.
echo Installation completed successfully!
pause